package com.eight.cloud.rabbitmq.util;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;

/**
 * 消息处理工具
 *
 * @ClassName: MessageUtils
 * @Author: TXC
 * @Date: 2025-09-01 14:47
 **/
public class MessageUtils {
    /**
     * 获取消息的channel
     * @param message 消息
     * @return 消息的channel
     */
    public static Channel getChannel(Message<?> message) {
        Channel channel = null;
        MessageHeaders headers = message.getHeaders();
        if (headers.containsKey(AmqpHeaders.CHANNEL)) {
            channel = (Channel) headers.get(AmqpHeaders.CHANNEL);
        }
        return channel;
    }

    /**
     * 获取消息的deliveryTag
     * @param message 消息
     * @return 消息的deliveryTag
     */
    public static Long getDeliveryTag(Message<?> message) {
        Long deliveryTag = null;
        MessageHeaders headers = message.getHeaders();
        if (headers.containsKey(AmqpHeaders.DELIVERY_TAG)) {
            deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        }
        return deliveryTag;
    }
}
